import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { getToken } from '@/utils/auth'
var config = require('../base-config.js');
const headers = {};

// pages/login/login

export default function request(params){
	headers['Content-Type'] = 'application/json';
	headers['X-Token'] = getToken()
	console.log('header', console.log(JSON.stringify(headers)));

	if(!params.method){
		params.method  = 'POST'
	}
	let url = params.url;
	
	 return new Promise((resolve, reject) => {
			 uni.request({
				 url: config.default.baseUrl + params.url, //仅为示例，并非真实接口地址。
				 data: params.data,
				 header: headers,
				method: params.method,
				success: res => {
					console.log('返回', console.log(JSON.stringify(res)));
					console.log('code', res.data.code);
					if(res.data.code == 401){
						console.log('跳轉', '/pages/login/login');
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}
					resolve(res.data);
				},
				fail: res =>{
					//失败时候回调
					uni.reLaunch({
						url: '/pages/login/login'
					})
					console.log('錯誤返回', console.log(JSON.stringify(res)));
					reject(res);
				}
			})
		 });
}
